﻿namespace Ex03.GarageLogic
{
    public class FuelEngine
    {
        public enum eFuelType
        {
            Diesel,
            Octan95,
            Octan96,
            Octan98
        }

        private eFuelType m_FuelType;
        private float m_CurrentFuelLevel;
        private float m_MaximumFuelLevel;

        public FuelEngine(eFuelType i_FuelType, float i_CurrentFuelLevel, float i_MaximumFuelLevel)
        {
            if (i_MaximumFuelLevel < i_CurrentFuelLevel)
            {
                throw new ValueOutOfRangeException("Fuel Engine", 0.0f, i_MaximumFuelLevel);
            }

            m_FuelType = i_FuelType;
            m_CurrentFuelLevel = i_CurrentFuelLevel;
            m_MaximumFuelLevel = i_MaximumFuelLevel;
        }

        public void Refuel(float i_LitresToAdd, eFuelType i_FuelType, ref Vehicle io_Vehicle)
        {
            if (i_LitresToAdd + CurrentFuelLevel > MaximumFuelLevel)
            {
                throw new ValueOutOfRangeException("Fuel Engine", 0.0f, MaximumFuelLevel - CurrentFuelLevel);
            }

            if (i_FuelType != m_FuelType)
            {
                throw new System.ArgumentException("Wrong fuel type at Refuel()", "i_FuelType");
            }

            m_CurrentFuelLevel += i_LitresToAdd;
            try
            {
                io_Vehicle.RemainingEnergyPercentage = ((CurrentFuelLevel / MaximumFuelLevel) * 100).ToString();
            }
            catch (ValueOutOfRangeException)
            {
                throw new ValueOutOfRangeException("Fuel Engine", 0.0f, MaximumFuelLevel - CurrentFuelLevel);
            }
        }

        public eFuelType FuelType
        {
            get { return m_FuelType; }
            set { m_FuelType = value; }
        }

        public float CurrentFuelLevel
        {
            get { return m_CurrentFuelLevel; }
            set { m_CurrentFuelLevel = value; }
        }

        public float MaximumFuelLevel
        {
            get { return m_MaximumFuelLevel; }
            set { m_MaximumFuelLevel = value; }
        }

        public override string ToString()
        {
            System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();

            stringBuilder.Append("Fuel Type: ");
            stringBuilder.AppendLine(System.Enum.GetName(typeof(eFuelType), m_FuelType).ToString());
            stringBuilder.Append("Current Fuel Level: ");
            stringBuilder.AppendLine(m_CurrentFuelLevel.ToString());
            stringBuilder.Append("Maximum Fuel Level: ");
            stringBuilder.AppendLine(m_MaximumFuelLevel.ToString());

            return stringBuilder.ToString();
        }
    }
}
